package com.gitee.feizns.quickstart.ding.talk.api.v10.yida.forms.instances;

import com.aliyun.dingtalkyida_1_0.Client;
import com.aliyun.dingtalkyida_1_0.models.SearchFormDataIdListHeaders;
import com.aliyun.dingtalkyida_1_0.models.SearchFormDataIdListRequest;
import com.aliyun.dingtalkyida_1_0.models.SearchFormDataIdListResponse;
import com.aliyun.dingtalkyida_1_0.models.SearchFormDataIdListResponseBody;
import com.aliyun.teautil.models.RuntimeOptions;
import com.gitee.feizns.quickstart.ding.talk.client.Clients;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 *
 * 获取多个表单实例ID（/v1.0/yida/forms/instances/ids/{appType}/{formUuid}）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/obtain-the-ids-of-multiple-form-instances">获取多个表单实例ID（/v1.0/yida/forms/instances/ids/{appType}/{formUuid}）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/obtain-the-ids-of-multiple-form-instances">服务端API 宜搭应用开发 表单 获取多个表单实例ID</a>
 * @author feizns
 * @since 2024/3/24
 */
public class Ids {

    /**
     * 获取多个表单实例ID
     *
     * searchFormJson：数字仅支持区间搜索
     * {
     *     "textField_jcr0069m": "danhang",
     *     "textareaField_jcr0069n": "duohang",
     *     "numberField_jcr0069o": [
     *         "1",
     *         "10"
     *     ],
     *     "radioField_jcr0069p": "选项一",
     *     "selectField_jcr0069q": "选项一",
     *     "checkboxField_jcr0069r": [
     *         "选项二"
     *     ],
     *     "multiSelectField_jcr0069s": [
     *         "选项二",
     *         "选项三"
     *     ],
     *     "dateField_jcr0069t": [
     *         1514736000000,
     *         1517414399000
     *     ],
     *     "cascadeDate_jcr0069u": [
     *         [
     *             1514736000000,
     *             1517414399000
     *         ],
     *         [
     *             1514736000000,
     *             1517414399000
     *         ]
     *     ],
     *     "employeeField_jcr0069x": [
     *         "xxxxx"
     *     ],
     *     "citySelectField_jcr0069y": [
     *         "110000",
     *         "110100",
     *         "110101"
     *     ],
     *     "departmentField_jcr0069z": 1123456,
     *     "cascadeSelectField_jcr006a0": [
     *         "part",
     *         "part_b"
     *     ],
     *     "tableField_jcr006a1": "明细数据"
     * }
     *
     * @param accessToken 访问凭据
     * @param formUuid 表单编号
     * @param req {@link SearchFormDataIdListRequest}
     * @return {@link SearchFormDataIdListResponseBody}
     */
    @SneakyThrows
    public static SearchFormDataIdListResponseBody  execute(@NotNull String accessToken,
                                                      @NotNull String appType,
                                                      @NotNull String formUuid,
                                                      @Nullable SearchFormDataIdListRequest req) {
        Client client = Clients.get(Client.class);
        SearchFormDataIdListResponse resp = client.searchFormDataIdListWithOptions(appType, formUuid, req, headers(accessToken), new RuntimeOptions());
        return resp.getBody();
    }

    /**
     * 获取多个表单实例ID
     * @param accessToken 访问凭据
     * @param formUuid 表单编号
     * @param req {@link SearchFormDataIdListRequest}
     */
    @SneakyThrows
    public static void executeAll(@NotNull String accessToken,
                                  @NotNull String appType,
                                  @NotNull String formUuid,
                                  @NotNull SearchFormDataIdListRequest req,
                                  @NotNull Consumer<String> idConsumer) {
        int pageNumber = 1;
        int pageSize = 100;
        long total;
        do {
            req.setPageSize(pageSize);
            req.setPageNumber(pageNumber);
            SearchFormDataIdListResponseBody body = execute(accessToken, appType, formUuid, req);
            total = body.getTotalCount();
            body.getData().forEach(idConsumer);
        } while ( total > (long) pageNumber++ * pageSize );
    }

    /**
     * 获取多个表单实例ID
     * @param accessToken 访问凭据
     * @param formUuid 表单编号
     * @param req {@link SearchFormDataIdListRequest}
     * @return {@link SearchFormDataIdListResponseBody}
     */
    @SneakyThrows
    public static List<String> executeAll(@NotNull String accessToken,
                                          @NotNull String appType,
                                          @NotNull String formUuid,
                                          @NotNull SearchFormDataIdListRequest req) {
        List<String> results = new ArrayList<>();
        executeAll(accessToken, appType, formUuid, req, results::add);
        return results;
    }

    /**
     * @param accessToken 访问凭据
     * @return {@link SearchFormDataIdListHeaders}
     */
    public static SearchFormDataIdListHeaders headers(@NotNull String accessToken) {
        SearchFormDataIdListHeaders searchFormDataIdListHeaders = new SearchFormDataIdListHeaders();
        searchFormDataIdListHeaders.xAcsDingtalkAccessToken = accessToken;
        return searchFormDataIdListHeaders;
    }

}
